var keyword:String;

var bookPageNum:Number;
var bookPageTotal:Number;
var currentSearchType:String;

var booksResult:Object;

function init()
{
	Packages.model.Context.activeContent=this;

	// UI.init
	var basic=Packages.system.mlf.GateKeeper.scanElement(this._name, "basic");
	Packages.system.display.JAccordion.initialize(basic);

	// Render.init
	Packages.system.render.Filter.titleFilter(Packages.system.mlf.GateKeeper.scanElement(this._name, "bookListTitle"));
	Packages.system.render.Filter.titleFilter(Packages.system.mlf.GateKeeper.scanElement(this._name, "bookPageTitle"));
	Packages.system.display.SceneTransition.rollOut();

	// Event.init
	var evtTargetArr:Array=new Array();
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "BookList_print"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "back_print"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "bookListPreSpot"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "bookListNextSpot"));
	Packages.system.events.StateFulButtonEvent.initialize(evtTargetArr);

	invokeInitComplete();
}

function dispose()
{
	Packages.system.mlf.GateKeeper.removeListener("onReleaseButtonEvent", this);

	delete keyword;
	delete bookPageTotal;
	delete bookPageNum;
	delete booksResult;
}

function invokeInitComplete()
{
	Packages.system.mlf.GateKeeper.addListener("onReleaseButtonEvent", this);

	// keyword, default search select
	keyword=Packages.model.Context.address.keyword;
	var selectSpot;
	if (keyword!=null)
	{
		Packages.model.Context.address.keyword=null;
		// select the search spot
		selectSpot=Packages.system.mlf.GateKeeper.scanElement(this._name, "resultSpot");
	}
	else 
	{
		// select the play spot
		selectSpot=Packages.system.mlf.GateKeeper.scanElement(this._name, "playSpot");
	}
	var param=mlf.mcmanager.PathResolver.drawPath(selectSpot);
	Packages.system.events.StateFulButtonEvent.invokeReleaseEvent(param, data, this._name);

	bookPageNum=1;
	var bookListCurrentPage=Packages.system.mlf.GateKeeper.scanElement(this._name, "bookListCurrentPage");
	bookListCurrentPage.text=bookPageNum;
	var tf_fmt:TextFormat=new TextFormat();
	tf_fmt.align="right";
	bookListCurrentPage.setTextFormat(tf_fmt);
}

function searchBookList(param:String, data:Object)
{
	//Packages.system.display.Progress.createInstance(Packages.system.mlf.GateKeeper.scanElement(this._name, "bookListContainer"));

	currentSearchType=param;

	var method:String="";
	if (param=="playSpot") method="books/playTop";
	else if (param=="rateSpot") method="books/rateTop";
	else if (param=="addSpot") method="books/latest";
	else if (param=="recommendSpot") method="books/playTop";
	else if (param=="resultSpot")
	{
		method="books/search/"+escape(keyword);
	}

	//
	if (data.pageNum==null)
	{
		bookPageNum=1;
	}
	else bookPageNum=data.pageNum;

	startIndex=(bookPageNum-1)*8;

	var bookListContainer=Packages.system.mlf.GateKeeper.scanElement(this._name, "bookListContainer");
	Packages.system.events.StateFulButtonEvent.removeSelectedSpot(bookListContainer);	//
	Packages.system.display.Progress.createInstance(bookListContainer);

	var bookListCurrentPage=Packages.system.mlf.GateKeeper.scanElement(this._name, "bookListCurrentPage");
	bookListCurrentPage.text=bookPageNum;
	var tf_fmt:TextFormat=new TextFormat();
	tf_fmt.align="right";
	bookListCurrentPage.setTextFormat(tf_fmt);

	Packages.modula.RequestCommand.request("HTTP", "restlet/"+method+"?startIndex="+startIndex+"&count=8", null, Packages.model.vo.BookListView, "detail", this, {container:"bookListContainer", slot:this._name, uri:"app/content/bookPage/BookList.xml", saved:"booksResult", initMethod:"lib:notes.components.content.bookPage.BookList.onBooksContainerInit"});
}

function onBooksContainerInit(param, data, slot)
{
	// Packages.log("onBooksContainerInit...", "debug");
	// UI.init
	var bookListContainer=Packages.system.mlf.GateKeeper.scanElement(this._name, "bookListContainer");
	Packages.system.display.JAccordion.initialize(bookListContainer, true);

	var rstCollection=booksResult.books?booksResult.books:booksResult.objects;	// @tmp no objects better
	var processBookId;
	var coverContainer;
	for (var i=0; i<rstCollection.length; i++)
	{
		processBookId=rstCollection[i].bookId;
		coverContainer=Packages.system.mlf.GateKeeper.scanElement(this._name, "bookCover_"+processBookId);
		var listener:Object=new Object();
		listener.onLoadInit=function(target_mc)
		{
			var minWidth:Number=64;
			var minHeight:Number=80;
			var zoom=Packages.system.utils.ImageUtil.minSize(target_mc, minWidth, minHeight);
			target_mc._xscale=zoom;
			target_mc._yscale=zoom;
			// centralize the target
			var offsetX:Number=(target_mc._width-minWidth)/2;
			target_mc._x=-offsetX;
			var offsetY:Number=(target_mc._height-minHeight)/2;
			target_mc._y=-offsetY;
		}
		if (rstCollection[i].image!=null)
		{
			mlf.mcmanager.dynamicloading.LoadApp.loadAppLoader(rstCollection[i].image, coverContainer, "img", listener); 
		}
	}
}

function previousBookList()
{
	if (bookPageNum>1)
	{
		bookPageNum--;
		searchBookList(currentSearchType, {pageNum:bookPageNum});
	}
}

function nextBookList()
{
	if (bookPageNum<bookPageTotal)
	{
		bookPageNum++;
		searchBookList(currentSearchType, {pageNum:bookPageNum});
	}
}

function onReleaseButtonEvent(evt:Object)
{
	if (evt.param=="playSpot")
	{
		searchBookList(evt.param);
	}
	else if (evt.param=="rateSpot")
	{
		searchBookList(evt.param);
	}
	else if (evt.param=="addSpot")
	{
		searchBookList(evt.param);
	}
	else if (evt.param=="recommendSpot")
	{
		searchBookList(evt.param);
	}
	else if (evt.param=="resultSpot")
	{
		searchBookList(evt.param);
	}
	else if (evt.param.indexOf("bookItem")>-1)
	{
		var selectedBookId=evt.param.substr(9);
		Packages.system.utils.ComponentsUtil.popupSelectedDataDetail(booksResult, selectedBookId, "BookDetail", "content/bookPage/BookSpecial");
	}
	else if (evt.param=="bookListPreSpot")
	{
		previousBookList();
	}
	else if (evt.param=="bookListNextSpot")
	{
		nextBookList();
	}
}

function handleResponseResult(rsp:Object)
{
	/*Packages.log("rsp.service.method: "+rsp.service.method, "debug");
	var rst=rsp.service.result;
	for (var a in rst)
	{
		Packages.log(a+": "+rst[a], "debug");
	}*/
	var dynamicPortlet=Packages.system.mlf.GateKeeper.scanElement(this._name, "bookListContainer");
	if (dynamicPortlet!=null)
	{
		// Packages.system.mlf.GateKeeper.updateDynamicPortlet(dynamicPortlet, null, rsp.vo.instance.content, null);
		for (var a in dynamicPortlet)
		{
			if (typeof(dynamicPortlet[a])=="movieclip")
			{
				dynamicPortlet[a].removeMovieClip();
			}
		}
		Packages.system.mlf.GateKeeper.loadML2byContent(rsp.vo.instance.content);
	}
	else Packages.system.mlf.GateKeeper.loadML2byContent(rsp.vo.instance.content);
	
	if (rsp.component.data.saved=="booksResult")
	{
		booksResult=rsp.service.result;
		var bookListTotalPage=Packages.system.mlf.GateKeeper.scanElement(this._name, "bookListTotalPage");
		bookPageTotal=Math.floor(rsp.service.result.count/8)+1;
		bookListTotalPage.text=bookPageTotal;
	}

	Packages.system.display.Progress.removeInstance(Packages.system.mlf.GateKeeper.scanElement(this._name, rsp.component.data.container));
}